/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ecom.dao;

import ecom.exception.DataAccessLayerException;
import ecom.entity.Customer;
import ecom.entity.Account;
import ecom.entity.Country;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.Expression;

/**
 *
 * @author simon
 */
public class CustomerDAO extends AbstractDAO{

    private static String LAST_NAME = "lastName";
    private static String ID_CUSTOMER = "idCustomer";
    private static String EMAIL = "email";

    /**
     * Créer un Customer dans la base de données
     * @param customer Customer à créer
     */
    public void create(Customer customer) throws DataAccessLayerException {
        super.saveOrUpdate(customer);
    }

    /**
     * Supprime un Customer dans la base de données
     * @param customer Customer à supprimer
     */
    public void delete(Customer customer) {
        super.delete(customer);
    }

    /**
     * Recherche tous les Customer de la base de données
     * @return List<Customer> Retourne une liste de Customer
     */
    public List<Customer> findAll() throws DataAccessLayerException{
        return super.findAll(Customer.class);
    }

    /**
     * Mettre à jour un Customer dans la base de données
     * @param customer Customer à mettre à jour
     */
    public void saveOrUpdate(Customer customer) {
        super.saveOrUpdate(customer);
    }

    /**
     * Recherche un Customer par nom
     * @param name nom du Customer
     * @return Customer Retourne un Customer
     */
    public Customer findByName(String name) {
        Customer customer = null;

        try {
            startOperation();
            Criteria crit = session.createCriteria(Customer.class);
            crit.add(Expression.eq(LAST_NAME, name));
            customer = (Customer) crit.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            handleException(e);
        }
        
        return customer;
    }
    
    /**
     * Recherche un Customer par id
     * @param id id du Customer
     * @return Customer Retourne un Customer
     */
    public Customer find(int id) {
        Customer customer = null;

        try {
            startOperation();
            Criteria crit = session.createCriteria(Customer.class);
            crit.add(Expression.eq(ID_CUSTOMER, id));
            customer = (Customer) crit.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            handleException(e);
        }
        
        return customer;
    }
    
    /**
     * Recherche un Customer par email
     * @param emai email du Customer
     * @return Customer Retourne un Customer
     */
    public Customer findByEmail (String email) {
        Customer customer = null;
        
        try {
            startOperation();
            Criteria crit = session.createCriteria(Customer.class);
            crit.add(Expression.eq(EMAIL, email));
            customer = (Customer) crit.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            handleException(e);
        }
        
        return customer;
        
    }

//    public static void main (String [] args) {
//        CustomerDAO dao = new CustomerDAO();
//
//        System.out.println("Recuperation des customers");
//        List<Customer> customers = dao.findAll(Customer.class);
//        for(Customer c : customers) {
//            System.out.println(c);
//        }
//
//        dao.closeSession();
//    }
}
